package cn.hnbjx.prepaid.controller;

import cn.hnbjx.prepaid.common.BusinessException;
import cn.hnbjx.prepaid.common.BusinessStatus;
import cn.hnbjx.prepaid.pojo.Account;
import cn.hnbjx.prepaid.pojo.WxParameter;
import cn.hnbjx.prepaid.service.AccountService;
import cn.hnbjx.prepaid.service.NewsService;
import cn.hnbjx.prepaid.service.WxService;
import cn.hnbjx.prepaid.utils.HttpRequestUtil;
import cn.hnbjx.prepaid.utils.JSONUtil;
import cn.hnbjx.prepaid.utils.Result;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@RestController
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private NewsService newsService;
    @Autowired
    WxParameter wxParameter;
    @Autowired
    private WxService wxService;

    private String openid;
    private String session_key;

    @GetMapping("/api/user/login")
    public Result login(String code,String nickName,String avatarUrl) {
        String mpAppid = wxParameter.getmpAppId();
        String mpAppsecret = wxParameter.getMpAppSecret();
        String URL = "https://api.weixin.qq.com/sns/jscode2session?appid="
                + mpAppid
                + "&secret=" + mpAppsecret
                + "&js_code=" + code
                + "&grant_type=authorization_code";
        String response = HttpRequestUtil.sendGet(URL);
        Map<String, Object> map = JSONUtil.jsonToMap(response);
        if (map.get("openid") == null) {
            throw new BusinessException(BusinessStatus.FAIL, "OPENID获取失败：" + map.get("errmsg"));
        }
        // 解析json
        session_key = map.get("session_key")+"";
        openid = map.get("openid")+"";
        System.out.println("session_key=="+session_key);
        System.out.println("openid=="+openid);
        return Result.ok(wxService.getMpAccountInfo(openid,nickName,avatarUrl));
    }

    @GetMapping("/api/user/auth")
    public Result auth(String username,String tel,String token) {
        accountService.updateAuth(username,tel,token);
        return Result.ok(accountService.findAccountById(token));
    }
    @GetMapping("/api/user/userInfo")
    public Result userInfo(String token) {
        System.out.println("token=="+token);
        return Result.ok(accountService.findAccountById(token));
    }
    @GetMapping("/api/user/applyAuth")
    public Result applyAuth(String name,String phone,String city,String address,String location,String token) {
        System.out.println("name=="+token);
        accountService.updateUser(name,phone,city,address,location,token);
        return Result.ok(accountService.findAccountById(token));
    }
    //
    @GetMapping("/api/Account/findAccount")
    public Result findAccountById(String openId){
        return Result.ok(accountService.findAccountById(openId));
    }

    @GetMapping("/api/Account/findAccount2")
    public Result findAccountById2(HttpServletRequest request){
        String openId=(String) request.getSession().getAttribute("openid");

        return Result.ok(accountService.findAccountById2(openId));
    }


    @GetMapping("/admin/getAccount")
    public Result getAccount(int page,String key){
        return Result.ok(accountService.getAccount(page,key));
    }



    @GetMapping("/admin/Account/audit")
    public Result audit(int id,String remark,int state,int audit){
        return Result.ok(accountService.audit(id,remark,state,audit));
    }

    @GetMapping("/api/user/invite")
    public void invite(String uniCode,HttpServletResponse response, HttpSession httpSession) throws IOException {
        httpSession.setAttribute("uniCode",uniCode);
        String redirect_uri = "http://" + wxParameter.getDomain() + "/api/login/mp/auth";
        response.sendRedirect(redirect_uri);
    }
    @GetMapping("/api/user/getCode")
    public Result getCode(HttpSession httpSession){
        String openId=(String) httpSession.getAttribute("openid");
        return Result.ok(accountService.getCode(openId));
    }

    @GetMapping("/api/news/list")
    public Result list(int page,int type){
        return Result.ok(newsService.apiallNews(page,type));
    }
}

